<?php

/**
 * 后台文章模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: article.php 111 2012-03-27 10:31:21Z zwt007 $
 * http://www.t-mac.org；
 */
class articleAction extends Action
{

    private $tmp_model;
    private $archives_model;
    private $tags_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('article');
        $this->archives_model = Tmac::model('archives');
        $this->tags_model = Tmac::model('tags');

        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin,tb_editor');
    }

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {
        $cat_id = intval($this->getParam('cat_id'));
        $channelid = intval($this->getParam('channelid'));

        $search_keyword = $this->H($this->getParam('search_keyword'));
        $do = $this->H($this->getParam('do'));

        $cat_id = empty($cat_id) ? 0 : $cat_id;
        $treers = $this->M('category')->getCategoryTreeList(0, $cat_id);

        //TODO  取出所有资讯
        $rs = $this->tmp_model->getArticleList($channelid, $cat_id, $search_keyword);

        //取友情操作类型radiobutton数组
        $article_do_ary = Tmac::config('article.do');
        $article_do_ary_option = Utility::Option($article_do_ary, $do);

        $this->assign('channelid', $channelid);
        $this->assign('treers', $treers);
        $this->assign('search_keyword', $search_keyword);
        $this->assign('article_do_ary_option', $article_do_ary_option);
        $this->assign($rs);        
        $this->V('article');
    }

    /**
     * 新增/修改文章页面
     */
    public function add()
    {
        $aid = intval($this->getParam('aid'));
        $channeltype = 1;
        $cat_id = intval($this->getParam('cat_id'));
        $uid = $_SESSION['admin_uid'];

        if ($aid > 0) {
            $editinfo = $this->tmp_model->getArticleInfo($aid);                        
            $editinfo['content'] = htmlspecialchars($editinfo['content']);
        } else {
            $editinfo = array(
                'title' => '',
                'uid' => '',
                'author' => '',
                'article_order' => '0',
                'content' => '',
                'cat_id' => $cat_id,
                'article_id' => '',
                'thumb' => '',
                'keywords' => '',
                'description' => '',
                'name' => '',
                'status' => 'publish',
                'comment_status' => 'open'
            );
        }
        //取文章栏目        
        //$treers = $this->tmp_model->article_cat_tree(0, 0, $editinfo['cat_id'], $channeltype);        
        $treers = $this->M('category')->getCategoryTreeList(0, $editinfo['cat_id']);

        //取作者数组
        $user_array = $this->tmp_model->getUserArray($uid);
        $user_array_option = Utility::Option($user_array, $editinfo['uid'], 'uid,nicename');

        //取文章状态数组
        $status_array = Tmac::config('article.status');
        $status_array_option = Utility::Option($status_array, $editinfo['status']);

        //取评论状态数组
        $comment_status_array = Tmac::config('article.comment_status');
        $comment_status_array_option = Utility::Option($comment_status_array, $editinfo['comment_status']);

        //取所有的Tag_info Array
        $tag_info_array = $this->tags_model->getTagsInfoArrayByArticle_id($aid);

        //初始化一下    默认state_radio
        $editinfo['author'] = !empty($editinfo['author']) ? $editinfo['author'] : '默认管理员';

        $this->assign('editinfo', $editinfo);
        $this->assign('treers', $treers);
        $this->assign('user_array_option', $user_array_option);
        $this->assign('user_array', $user_array);
        $this->assign('status_array_option', $status_array_option);
        $this->assign('comment_status_array_option', $comment_status_array_option);
        $this->assign('tag_info_array', $tag_info_array);
        //TODO　载入资讯类别添加表单
        $this->V('article');
    }

    /**
     * 新增/修改栏目页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 1) {
            $this->redirect('don\'t be evil');
            exit;
        }
        $separator = '&lt;--more--&gt;';
        /* 初始化变量 */
        $aid = intval($this->getParam('aid'));
        $cat_id = intval($this->getParam('cat_id'));
        $title = trim($this->H($this->getParam('title')));
        $author = trim($this->H($this->getParam('author')));
        $article_order = intval($this->getParam('article_order'));
        $thumb = $this->H($this->getParam('thumb'));
        $content = $this->getParam('content');        
        $keywords = $this->H($this->getParam('keywords'));
        $description = $this->H($this->getParam('description'));
        $uid = intval($this->getParam('uid'));
        $status = $this->H($this->getParam('status'));
        $comment_status = $this->H($this->getParam('comment_status'));
        $name = $this->H($this->getParam('name'));
        //判断有没有more的内容放到content_description
        if (strpos($content, $separator)) {
            list($content_descrption, $extand) = explode($separator, $content, 2);
            $content_descrption .= $separator;                        
        } else {
            $content_descrption = $content;
        }

        //Tags相关
        $tag_id_array = $this->getParam('tag_id');
        $tag_name_array = $this->getParam('tag_name');        

        $channelid = 1;
        $admin_uid = intval($_SESSION['admin_uid']);

        if (!$this->archives_model->checkChannel($cat_id, $channelid)) {
            $this->redirect("你所选择的栏目与当前模型不相符！");
            exit();
        }

        //判断是否为空
        if (empty($title)) {
            $this->redirect('标题不能力空');
            exit;
        }

        if (empty($content)) {
            $this->redirect('内容不能力空');
            exit;
        }

        $info = array(
            'cat_id' => $cat_id,
            'channel' => $channelid,
            'uid' => $uid,
            'title' => $title,
            'author' => $author,
            'article_order' => $article_order,
            'keywords' => $keywords,
            'description' => $description,
            'thumb' => $thumb,
            'status' => $status,
            'comment_status' => $comment_status,
            'name' => $name,
            'edit_time' => time(),
            'content_descrption' => $content_descrption
        );
        $info_addonarticle = array('cat_id' => $cat_id, 'content' => $content);

        $type_url = PHP_SELF . '?m=archives.arclist&channelid=' . $channelid . '&cat_id=' . $cat_id;

        if ($aid > 0) {
            //update save article
            $where = 'article_id = ' . $aid . '';
            $rs = $this->tmp_model->addArticle('article', $info, 'UPDATE', $where);
            //插入Tag_info            
            $this->tags_model->saveTagsInfo($aid, $tag_id_array, $tag_name_array);
            if ($rs) {
                $info_addonarticle['article_id'] = $aid;
                $rs = $this->tmp_model->addArticle('addon', $info_addonarticle, 'UPDATE', $where);
                if ($rs) {
                    $this->redirect('修改资讯文章成功', $type_url);
                } else {
                    $this->redirect('修改资讯文章从表失败，请联系技术支持检查原因！');
                }
            } else {
                $this->redirect('修改资讯文章失败');
            }
        } else {
            $info['time'] = time();
            //insert save article_class
            $article_id = $this->tmp_model->addArticleReturn($info);
            //插入Tag_info
            $this->tags_model->saveTagsInfo($article_id, $tag_id_array, $tag_name_array);
            if ($article_id) {
                $info_addonarticle['article_id'] = $article_id;
                $rs = $this->tmp_model->addArticle('addon', $info_addonarticle, 'INSERT');
                if ($rs) {
                    $this->redirect('添加资讯文章成功', $type_url);
                } else {
                    //把主表的新增加的删除掉
                    $this->tmp_model->delArticle($article_id);
                    $this->redirect('添加资讯文章从表失败，请联系技术支持检查原因！');
                }
            } else {
                $this->redirect('添加资讯文章主表失败，请联系技术支持检查原因！');
            }
        }
    }

    /**
     * 批量操作
     */
    public function article_do()
    {
        $do = $this->getParam('do');
        $id_a = $_POST['id_a'];

        if (is_array($id_a)) {
            $id = implode(',', $id_a);
        } else {
            $this->redirect('请选择要操作的...');
            exit;
        }

        if ($do == 'del') {
            $rs = $this->tmp_model->delArticle($id);
            // TODO DEL该分类下的所有资讯
            if ($rs) {
                $this->redirect('删除资讯成功');
            } else {
                $this->redirect('删除资讯失败，请重试！');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $aid = (int) $_GET['aid'];
        $id_a = $_POST['id_a'];
        if (is_array($id_a)) {
            $id = implode(',', $id_a);
        } elseif (!empty($aid)) {
            $id = $aid;
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->delArticle($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $this->redirect('删除资讯成功');
        } else {
            $this->redirect('删除资讯失败，请重试！');
        }
    }

}